<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/28 00:39
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Base_Model extends WX_Model
{

    protected $width = 200;
    protected $height = 200;

    private $baseId = 0;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取详情信息
     * @param $baseId
     * @return array|bool
     */
    public function getBaseDetailByBaseId($baseId)
    {
        $this->baseId = $baseId;
        $db = clone($this->db);
        $q = $db->select('name, content, thumb, liveUrl, streamId, pv, createdBy, created')
            ->where('id', $baseId)
            ->get('base');
        $row = $q->row_array();
        if (empty($row)) {
            $this->setError('农场不存在');
            return false;
        }

        // 修改PV
        $this->addPv('base', array('id' => $baseId), $row['pv']);

        $detail = array(
            'baseId' => intval($baseId),
            'baseName' => value($row, 'name'),
            'thumb' => thumb($row['thumb'], 640, 360),
            'isLive' => !empty($row['liveUrl']),
            'liveUrl' => value($row, 'liveUrl', ''),
            'pv' => intval($row['pv']) + 1,
            'created' => formateTimeStamp($row['created']),
            'content' => $row['content']
        );

        // 基地下的产品
        $this->_getGoodsInBase($detail);
        // 相关产品
        $this->_getRelatedInBase($detail);

        return $detail;
    }

    /**
     * 相关产品
     * @param $detail
     * @return bool
     */
    protected function _getRelatedInBase(&$detail)
    {
        $detail['related'] = array(
            'total' => 0,
            'data' => []
        );
        $db = clone($this->db);
        $q = $db->select('id, goodsName, thumb, liveUrl, bannerThumb, recommendThumb, thumbWidth, thumbHeight, streamId, price, created')
            ->from('base_goods')
            ->order_by('pv', 'DESC')
            ->limit(8)
            ->where(array('status' => 1))
            ->where('stock > 0')
            ->where_not_in('id', array($this->baseId))
            ->get();
        $rows = $q->result_array();
        if (empty($rows)){
            return false;
        }

        $results = $this->_doAfterSelectGoods($rows);
        $detail['related']['total'] = count($results);
        $detail['related']['data'] = $results;
    }

    /**
     * 该基地下的产品
     * @param $detail
     * @return bool
     */
    protected function _getGoodsInBase(&$detail)
    {
        $detail['goods'] = array(
            'total' => 0,
            'data' => []
        );
        $db = clone($this->db);
        $q = $db->select('id, goodsName, thumb, liveUrl, bannerThumb, recommendThumb, thumbWidth, thumbHeight, streamId, price, created')
            ->from('base_goods')
            ->order_by('likes', 'DESC')
            ->limit(4)
            ->where(array('status' => 1, 'baseId' => $this->baseId))
            ->where("stock > 0")
            ->get();
        $rows = $q->result_array();
        if (empty($rows)){
            return false;
        }

        $results = $this->_doAfterSelectGoods($rows, true);
        $detail['goods']['total'] = count($results);
        $detail['goods']['data'] = $results;
    }

    private function _doAfterSelectGoods($goods = array(), $selfGoods = false)
    {
        $results = [];
        foreach($goods as $item) {
            if ($selfGoods) {
                $width = 200;
                $height = 200;
                $thumb = !empty($item['recommendThumb']) ? $item['recommendThumb'] : $item['thumb'];
            } else {
                $wh = $this->countWidthHeight($item['thumbWidth'], $item['thumbHeight']);
                $width = (int) $wh['width'];
                $height = (int) $wh['height'];
                $thumb = $item['thumb'];
            }
            $thumb = thumb($thumb, $width, $height);
            $results[] = array(
                'goodsId' => intval($item['id']),
                'goodsName' => value($item, 'goodsName'),
                'thumb' => $thumb,
                'width' => $width,
                'height' => $height,
                'price' => money_format('%i', $item['price']),
                'created' => formateTimeStamp($item['created']),
                'isLive' => !empty($item['liveUrl']),
                'liveUrl' => value($item, 'liveUrl'),
            );
        }
        return $results;
    }

    protected function addPv($table = '', $where = array(), $pv = 0)
    {
        if (empty($pv)) {
            $rowDb = clone($this->db);
            $q = $rowDb->get_where($table, $where);
            $row = $q->row_array();
            if (empty($row)) {
                return false;
            }
            $pv = intval($row['pv']);
        }

        $db = clone($this->db);
        $db->where($where);
        $db->update($table, array('pv' => $pv + 1));
    }

}